package com.example.noteapp.Login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import com.example.noteapp.HomePager.HomeActivity;
import com.example.noteapp.R;
import com.example.noteapp.Util.UtilMethod;
import com.example.noteapp.databinding.ActivityLoginBinding;

public class LoginActivity extends AppCompatActivity {
    ActivityLoginBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initMethod();

    }

    private void initMethod() {
        binding.buttonLogin.setOnClickListener(view ->{
            String username = binding.inputUsername.getText().toString();
            String password = binding.inputPassword.getText().toString();
            //用户名验证
            if (username.isEmpty()) {
                binding.inputLayoutUsername.setError("username is empty!");
                return;
            } else {
                binding.inputLayoutUsername.setErrorEnabled(false);
            }
            //密码验证
            if (password.isEmpty()) {
                binding.inputLayoutPassword.setError("password is empty!");
                return;
            } else {
                binding.inputLayoutPassword.setErrorEnabled(false);
            }
            //登录验证
            if (username.equals("xixi") && password.equals("xixi...")) {
                //登录成功，跳转主页面
                startActivity(new Intent(this, HomeActivity.class));
                //显示登录成功提示
                UtilMethod.showToast(getApplicationContext(),"Login success");
                // 结束当前登录Activity
                finish();
            }else{
                // 登录失败，显示错误提示
                UtilMethod.showToast(getApplicationContext(),"username or password incorrect!");
            }
        });
    }
    }